angular.module('Controller')
.controller('QgyFloorManageEditCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    type=items.type
    $scope.type=items.type
    $scope.data=items.data
    $scope.info=info=items.info
    floorDesc=angular.copy($scope.info?.floorDesc)
    $scope.modalTitle=if type=='new' then '新建楼层' else '编辑楼层'
    $scope.modalEnableList=commonConstant.enabledList()
    _.defer(()->
        $('#thumb-upload-input').fileinput({
            language: 'zh',
            maxFileCount: 1,
            allowedFileExtensions : ['jpg', 'png','gif','jpeg'],
            uploadUrl: urls.backend + urls.imageUploadTo +  "/image?token=" + $localStorage['token'],
            uploadAsync: true,
            enctype: 'multipart/form-data',
            showCaption: false
        }).on('fileuploaded', (event, data, id, index)->
            if data.response?.code==0
                console.log(data.response?.message)
                $scope.result = data.response?.message
                saveUrl = $scope.result['saveUrl']
                console.log('saveUrl.indexOf(http)',saveUrl.indexOf("http"))
                if(saveUrl.indexOf("http")==0)
                    $scope.data.thumb = saveUrl
                else
                    $scope.data.thumb=urls.backend+'/public/'+$scope.result['saveUrl']

                $scope.showThumbPhotoUpload=false
                $scope.$digest()
            else
                wfAlert.toastError(data.response.message)
        )
    )
    _.extend $scope,
        newData:()->
            if $scope.data.id==undefined || parseInt($scope.data.id)<0
                wfAlert.toastInfo("楼层id不允许为空")
                return
            if _.some(floorDesc,(oriFl)->parseInt(oriFl.id)==parseInt($scope.data.id) || oriFl.name==$scope.data.name)
                wfAlert.toastInfo("楼层id,楼层名称已存在，请修改")
                return
            newFloorList=floorDesc
            newFloorList.push {id:parseInt($scope.data.id),name:$scope.data.name,thumb:$scope.data.thumb || '',enabled:true}
            $http.put("/building/#{$scope.info.buildingId}",{floorDesc:newFloorList})
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close(newFloorList)
                else
                    wfAlert.toastError(resp.message)
            )
        editData:()->
#            if _.some(oriFlData,(oriFl)->oriFl.id==$scope.data.id || oriFl.name==$scope.data.name)
#                wfAlert.toastInfo("楼层id,楼层名称已存在，请修改")
#                return
            newFloorList=angular.copy(floorDesc)
            editFloor=_.find(newFloorList,(theFloor)->theFloor.id==$scope.data.id)
            if editFloor==undefined
                wfAlert.toastError('数据异常')
                return
            editFloor.name=$scope.data.name
            editFloor.thumb=$scope.data.thumb
            editFloor.enabled=$scope.data.enabled
#            console.log(editFloor,newFloorList)
            $http.put("/building/#{$scope.info.buildingId}",{floorDesc:newFloorList})
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close(newFloorList)
                else
                    wfAlert.toastError(resp.message)
            )
        removeData:()->
            newFloorList=angular.copy(floorDesc)
            newFloorList=_.filter(newFloorList,(theFL)->parseInt(theFL.id)!=parseInt($scope.data.id))
            $http.put("/building/#{$scope.info.buildingId}",{floorDesc:newFloorList})
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('删除成功')
                    $uibModalInstance.close(newFloorList)
                else
                    wfAlert.toastError(resp.message)
            )
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)